<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BUINode
 *
 * @author jianfeng
 */
abstract class BUINode extends BUIBase {

    //put your code here
    protected $service = null;
    protected $sApp = null;
    protected $operation = null;
    protected $style = null;
    protected $menuManager = null;
    protected $bootStrapMenu = null;
    protected $menu = null;
    public $transitScopeName = 'UINode';

    public function __construct($service = null, $sApp = null, $operation = null) {

        parent::__construct();

        if ($service != null)
            $this->service = $service;
        if ($sApp != null)
            $this->sApp = $sApp;
        if ($operation != null)
            $this->operation = $operation;
    }

    public function getId() {
        $id = '';

        if ($this->getActiveService() != NULL)
            $id .= $this->service->getId();

        if ($this->getActiveServiceApp() != NULL)
            $id .= '_' . $this->sApp->getId();

        if ($this->getActiveOperation() != NULL)
            $id .= '_' . $this->operation->getId();

        if ($id == "")
            return '000';

        return $id;
    }

    /**
     *
     * @return BService 
     */
    public function getActiveService() {
        return $this->service;
    }

    /**
     *
     * @return BServiceApp 
     */
    public function getActiveServiceApp() {
        return $this->sApp;
    }

    /**
     *
     * @return BOperation 
     */
    public function getActiveOperation() {
        return $this->operation;
    }

    public function setActiveService($service) {
        $this->service = $service;
    }

    public function setActiveServiceApp($app) {
        $this->sApp = $app;
    }

    public function setActiveOperation($opp) {
        $this->operation = $opp;
    }

    public abstract function render($config = null);

    public abstract function getUrl($data);

    public function addScreenActionGroup($name, $screenAction) {

        $screenActionStack = $this->getTransitVariable('screenActionStack');

        if ($screenActionStack == NULL) {
            $screenActionStack = array();
        }

        if (!isset($screenActionStack[$name]) || $screenActionStack[$name] == NULL) {
            $screenActionStack[$name] = array();
        }

        array_push($screenActionStack[$name], $screenAction);

        $this->addTransitVariable('screenActionStack', $screenActionStack);
    }

    protected function renderNode($screenflow = null, $screen = null, $data = null) {
        $controller = yii::app()->getController();
        if (!is_array($data))
            $data = array();
        $data['uiNode'] = $this;
        $this->resetTransitVariables($this->getId(), $this->transitScopeName);
        $controller->render('viewNode', array('uiNode' => $this,
            'screenflow' => $screenflow,
            'screen' => $screen,
            'data' => $data));
    }

    public function getScreenActionGroup($name) {
        $screenActionStack = $this->getTransitVariable('screenActionStack');
        return $screenActionStack[$name];
    }

    public function addTransitVariable($name, $value, $scope = NULL, $type = NULL) {       

        if ($scope == NULL)
            $scope = $this->transitScopeName . '_' . $this->getId();       

        //$this->resetTransitVariables($scope, $this->transitScopeName);
        parent::addTransitVariable($name, $value, $scope, $type);
    }

    public function getTransitVariable($name, $scope = NULL, $type = NULL) {
        if ($scope == NULL)
            $scope = $this->transitScopeName . '_' . $this->getId();

        //$this->resetTransitVariables($scope, $this->transitScopeName);
        $value = parent::getTransitVariable($name, $scope, $type);
        return $value;
    }

    protected function registerMenu($event) {
        //initialize menus for site from store
        $savedSiteMenu = Blyn::app()->getCurrentUser()->getTransitVariable('SiteMenu');

        if ($savedSiteMenu != NULL)
            yii::app()->getController()->menu = $savedSiteMenu;
        else {
            Blyn::app()->getCurrentUser()->addTransiteVariable('SiteMenu', yii::app()->getController()->menu);
        }
    }

    /*
      public function onBeforeRenderNode($event) {
      $this->raiseEvent('onBeforeRenderNode', $event);
      }


      protected function resetUINodeOnSwitchUINode($event) {
      $uiNode = $event->sender;

      $this->resetTransitVariables($uiNode->getId(), $uiNode->transitScopeName);
      }
     * 
     */
}

?>
